<?php

namespace app\api\model;

use think\Model;

class Product extends Model
{
    protected $login_rule =   [
        'username'   => 'require|between:5-15',
        'password'   => 'require|between:5-15',
    ];

    protected $login_message  =   [
        'username.require' => '名称必须',
        'username.between' => '账号长度在5-15之间',
        'password.require' => '请输入登录密码',
        'password.between' => '密码长度在5-15之间'
    ];

    public function login_vali($data)
    {

        try {

            validate($this->login_rule,$this->login_message)->check($data);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }

    protected $add_rule =   [
        'name'       => 'require',
        'company_id'=> 'require',
        'product_type_id'   => 'require',
        'specifications'   => 'require',
        'time_limit' => 'require',
    ];

    protected $add_message  =   [
        'name.require' => '请输入产品品牌名称',
        'company_id.require' => '请输选择所属单位',
        'product_type_id.require' => '请选择产品类型',
        'specifications.require' => '请输入规格',
        'time_limit.require' => '请输入产品保质期'
    ];

    public function add_vali($data)
    {
        $info = $this->where('name',$data['name'])->find();
        if($info['company_id'] == $data['company_id']){
            return re_warning('已经存在该产品');
        }

        try {
            validate($this->add_rule,$this->add_message)->check($data);
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }
    public function add($data)
    {
        $this->startTrans();
        try {

            $this->allowField([
                'company_id',
                'product_type_id',
                'name',
                'vr_url',
                'time_limit',
                'specifications',
                'image',
                'images',
                'introduce',
                'characteristic',
                'content',
                'status',
                'user_id',
                'time',
                'characteristic_address',
                'sc_time',
                'protection_scope',
                'protection_image'

            ])->save($data);
            $this->commit();
            return re_success("添加成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $this->rollback();
            return re_error($e->getMessage());
        }
    }

    protected $edit_rule =   [
        'id'   => 'require',
        'company_id'   => 'require',
    ];

    protected $edit_message  =   [
        'id.require'=>'缺失的产品',
        'company_id.require'=>'缺失的产品单位',

    ];

    public function edit_vali($data)
    {

        try {
            validate($this->edit_rule,$this->edit_message)->check($data);
            $info = $this->find($data['id']);
            if($info['company_id'] !== $data['company_id']){
                return re_warning('已记录非法修改他人产品');
            }
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }

    public function edit($data)
    {

        $model = Product::find($data['id']);
        $model->startTrans();
        try {
            $model->allowField([
                'product_type_id',
                'name',
                'vr_url',
                'time_limit',
                'specifications',
                'image',
                'images',
                'introduce',
                'characteristic',
                'content',
                'status',
                'user_id',
                'time',
                'characteristic_address',
                'sc_time',

                'protection_scope',
                'protection_image'
            ])->save($data);
            $model->commit();

            return re_success("修改成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return re_error($e->getMessage());
        }
    }
    public function editStatus($data)
    {

        $model = Product::find($data['id']);
        $info = $this->find($data['id']);
        if($info['company_id'] !== $data['company_id']){
            return re_warning('已记录改变他人产品状态');
        }
        $model->startTrans();
        try {
            $model->allowField([
                'status'
            ])->save($data);
            $model->commit();
            return re_success("操作成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return re_error($e->getMessage());
        }
    }

    protected $dele_rule =   [
        'id'   => 'require',
        'company_id'=>'require',
    ];

    protected $dele_message  =   [
        'id.require'=>'缺失的参数',
        'company_id.require'=>'缺失的产品单位',
    ];
    public function dele_vali($data)
    {
        try {
            validate($this->dele_rule,$this->dele_message)->check($data);
            $info = $this->find($data['id']);
            if($info['company_id'] !== $data['company_id']){
                return re_warning('已记录非法删除他人产品');
            }
        } catch (\Exception $e) {
            // 验证失败 输出错误信息
            return re_warning($e->getMessage());
        }
    }
    public function dele($id)
    {
        $model = Product::find($id);

        $model->startTrans();
        try {
            $model->delete();
            $model->commit();
            return re_success("删除成功");
            // 提交事务
        } catch (\Exception $e) {
            // 回滚事务
            $model->rollback();
            return re_error($e->getMessage());
        }
    }
}